package com.lonelyzhe.springboot.controller;

import com.lonelyzhe.springboot.bean.OrderInfo;
import com.lonelyzhe.springboot.bean.OrderItem;
import com.lonelyzhe.springboot.service.impl.OrderInfoService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("orderInfo")
@ResponseBody
public class OrderInfoController extends BaseController {

    @Autowired
    OrderInfoService orderInfoService;

    @RequestMapping("/list")
    public AjaxResult list(@RequestParam("user_id") int user_id) {
        try {
            List<OrderInfo> orderInfos = orderInfoService.selectOrderList(user_id);
            return AjaxResult.success(orderInfos);
        } catch (Exception e) {
            e.printStackTrace();
            // 默认错误是500
            return AjaxResult.error("发生错误");
            // return error(AjaxResult.Type.WARN, "发生错误");
        }
    }
    @RequestMapping("/getOrderDetails")
    public AjaxResult getOrderDetails(@RequestParam("id") int id) {
        try {
            OrderInfo orderInfo = orderInfoService.getOrderDetails(id);
            return AjaxResult.success(orderInfo);
        } catch (Exception e) {
            e.printStackTrace();
            // 默认错误是500
            return AjaxResult.error("发生错误");
            // return error(AjaxResult.Type.WARN, "发生错误");
        }
    }
    @RequestMapping("/listWithoutPay")
    public AjaxResult listWithoutPay(@RequestParam("user_id") int user_id) {
        try {
            List<OrderInfo> orderInfos = orderInfoService.selectOrderListWithoutPay(user_id);
            return AjaxResult.success(orderInfos);
        } catch (Exception e) {
            e.printStackTrace();
            // 默认错误是500
            return AjaxResult.error("发生错误");
            // return error(AjaxResult.Type.WARN, "发生错误");
        }
    }
    @RequestMapping("/listWithPay")
    public AjaxResult listWithPay(@RequestParam("user_id") int user_id) {
        try {
            List<OrderInfo> orderInfos = orderInfoService.selectOrderListWithPay(user_id);
            return AjaxResult.success(orderInfos);
        } catch (Exception e) {
            e.printStackTrace();
            // 默认错误是500
            return AjaxResult.error("发生错误");
            // return error(AjaxResult.Type.WARN, "发生错误");
        }
    }
    @RequestMapping("/updateToPay")
    public AjaxResult updateToPay(@RequestParam("id") int id) {
        try {
            String result = orderInfoService.updateToPay(id);
            if(result.equals("success")){
                return AjaxResult.success();
            }else{
                return AjaxResult.warn("此订单的状态不正确");
            }
        } catch (Exception e) {
            e.printStackTrace();
            // 默认错误是500
            return AjaxResult.error("发生错误");
            // return error(AjaxResult.Type.WARN, "发生错误");
        }
    }
    @RequestMapping("/updateToCancel")
    public AjaxResult updateToCancel(@RequestParam("id") int id) {
        try {
            String result = orderInfoService.updateToCancel(id);
            if(result.equals("success")){
                return AjaxResult.success();
            }else{
                return AjaxResult.warn("此订单的状态不正确");
            }
        } catch (Exception e) {
            e.printStackTrace();
            // 默认错误是500
            return AjaxResult.error("发生错误");
            // return error(AjaxResult.Type.WARN, "发生错误");
        }
    }
}
